/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    const int INF=0x3f3f3f3f;
    vector<int> largestValues(TreeNode* root) {
        if(root==nullptr) return {};
        queue<TreeNode*> q;
        vector<int> ans;
        int cnt=1;
        q.push(root);
        while(!q.empty())
        {    
            int mx=-2147483648;
            while(cnt--)
            {
                auto u=q.front(); q.pop();
                mx=max(mx, u->val);
                if(u->left) q.push(u->left);
                if(u->right) q.push(u->right);
            }
            cnt=q.size();
            ans.emplace_back(mx);
        }
        return ans;
    }
};